package com.cn.demo4.service.impl;

import com.cn.demo4.dao.NavDao;
import com.cn.demo4.dao.impl.NavDaoImpl;
import com.cn.demo4.model.NavModel;
import com.cn.demo4.service.INavService;
import com.cn.demo4.util.Tool;
import redis.clients.jedis.Jedis;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class NavServiceImpl implements INavService {
    @Override
    public List<NavModel> findAll(NavModel navModel) {
        NavDao navDao = new NavDaoImpl();
        return navDao.findAll(navModel);
    }

    @Override
    public String add(NavModel navModel) {
        NavDao navDao = new NavDaoImpl();
        int result = navDao.add(navModel);
        if(result == 1){
            Jedis jedis = new Jedis();
            //清空缓存
            jedis.del("navId");
            jedis.del("navName");
            jedis.del("navHref");
            jedis.del("navContent");
            jedis.del("navImg");
            jedis.del("navStatus");
            jedis.del("navTypeName");
//            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public NavModel findById(Integer id) {
        NavDao navDao = new NavDaoImpl();
        return navDao.findById(id);
    }

    @Override
    public String update(NavModel navModel) {
        NavDao navDao = new NavDaoImpl();
        int result = navDao.update(navModel);
        if(result == 1) {
            Jedis jedis = new Jedis();
            jedis.del("navId");
            jedis.del("navName");
            jedis.del("navHref");
            jedis.del("navContent");
            jedis.del("navImg");
            jedis.del("navStatus");
            jedis.del("navTypeName");
//            jedis.flushDB();
        }
        return result == 1 ? "success" : "error";
    }

    @Override
    public int del(Integer id) {
        NavDao navDao = new NavDaoImpl();
        int result = navDao.del(id);
        if(result == 1) {
            Jedis jedis = new Jedis();
//            jedis.flushDB();
            jedis.del("navId");
            jedis.del("navName");
            jedis.del("navHref");
            jedis.del("navContent");
            jedis.del("navImg");
            jedis.del("navStatus");
            jedis.del("navTypeName");
        }
        return result;
    }
    @Override
    public int getCount(NavModel navModel) {
        NavDao navDao = new NavDaoImpl();
        return navDao.getCount(navModel);
    }
    /**
     * 缓存redis
     * @return
     */
    @Override
    public  List<NavModel> getNavModelListForHome(){
        List<NavModel> list = new ArrayList<>();
        Jedis jedis = new Jedis();
        NavDao navDao = new NavDaoImpl();
        Boolean exists = jedis.exists("navId");
        if(exists){
            List<String> navIdList = jedis.lrange("navId",0,-1);
            for(int i =0; i < navIdList.size(); i++){
                NavModel navModel = new NavModel();
                navModel.setId(Tool.stringToInt(jedis.lindex("navId",i)));
                navModel.setName(jedis.lindex("navName",i));
                navModel.setHref(jedis.lindex("navHref",i));
                navModel.setContent(jedis.lindex("navContent",i));
                navModel.setImg(jedis.lindex("navImg",i));
                navModel.setStatus(Tool.stringToInt(jedis.lindex("navStatus",i)));
                navModel.setTypeName(jedis.lindex("navTypeName",i));
                list.add(navModel);
            }
            System.out.println("mysql");

        }else{
            //查询到数据保存到redis中
            list = navDao.getNavModelListForHome();
            for(int i = 0 ; i<list.size();i++){
                jedis.rpush("navId",Integer.toString(list.get(i).getId()));
                jedis.rpush("navName",list.get(i).getName());
                jedis.rpush("navHref",list.get(i).getHref());
                jedis.rpush("navContent",list.get(i).getContent());
                jedis.rpush("navImg",list.get(i).getImg());
                jedis.rpush("navStatus", Integer.toString(list.get(i).getStatus()));
                jedis.rpush("navTypeName",list.get(i).getTypeName());
            }
            System.out.println("redis");

        }
        return list;
    }

}
